<%@ page language="java" errorPage="/error.jsp" pageEncoding="UTF-8"
	contentType="text/html;charset=UTF-8"%>
<%@ include file="/includes/taglibs.jsp"%>

 <%
 String ctxPath = request.getContextPath();
 	
 %>
<html>
<head>
<title>文件扫描</title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" type="text/css" href="<c:url value='/styles/console.css'/>" />
<script type="text/javascript" src="<c:url value='/scripts/jquery.js'/>"></script>
	
<object classid = "clsid:5220cb21-c88d-11cf-b347-00aa00a28331" VIEWASTEXT>
	<param name="LPKPath" value="<%= ctxPath%>/cabs/DynamicWebTwain.lpk" />
</object>
</head>
<body onload="init();">
<% 
	String scanImagePath = ctxPath+"/common/images/cabs"; 
    String fileType = request.getParameter("fileType");
     String maxImages = request.getParameter("maxImages");
    if(fileType==null)
    {
     	fileType="jpg";
    }
    if(maxImages==null)
    {
     	maxImages="2";
    }
%>
            
<table border="0" align="center" width="100%">
 <tr>
   <td align="center" bgcolor="gray"> 
     <object classid="clsid:E7DA7F8D-27AB-4EE9-8FC0-3FEC9ECFE758" id="webTwain" height="300" width="500"
	CodeBase = "<%= ctxPath%>/cabs/DynamicWebTWAIN.cab#version=6.1">
		<param name="_cx" value="847" />
		<param name="_cy" value="847" />
		<param name="JpgQuality" value="80" />
		<param name="Manufacturer" value="Dynamsoft Corporation" />
		<param name="ProductFamily" value="Dynamic Web TWAIN" />
		<param name="ProductName" value="Dynamic Web TWAIN" />
		<param name="VersionInfo" value="Dynamic Web TWAIN 6.1" />
		<param name="TransferMode" value="0" />
		<param name="BorderStyle" value="0" />
		<param name="FTPUserName" value="" />
		<param name="FTPPassword" value="" />
		<param name="FTPPort" value="21" />
		<param name="HTTPUserName" value="" />
		<param name="HTTPPassword" value="" />
		<param name="HTTPPort" value="80" />
		<param name="ProxyServer" value="" />
		<param name="IfDisableSourceAfterAcquire" value="0" />
		<param name="IfShowUI" value="1" />
		<param name="IfModalUI" value="1" />
		<param name="IfTiffMultiPage" value="0" />
		<param name="IfThrowException" value="0" />
		<param name="MaxImagesInBuffer" value="<%=maxImages %>" />
		<param name="TIFFCompressionType" value="0" />
	</object>
   </td>
 </tr>
 <tr>
   <td>
     <table border="0" class="EditTable">
       <tr>
         <td colspan="3" style="padding: 0px">
          <table style="border: none;padding: 0px">
            <tr>
        
              <td style="text-align: left;border: none;">
                <input type="button" title="上一张" disabled id="btPrevious" style="filter:Gray;width: 26;height: 26;padding: 0px;background: url('<%=scanImagePath %>/previousImg.gif');" onclick="previous();">
                <input type="button" title="下一张" disabled id="btNext" style="filter:Gray;width: 26;height: 26;padding: 0px;background: url(<%=scanImagePath %>/nextImg.gif);" onclick="next();">
                <input type="button" disabled title="逆时针旋转" id="btRotateLeft" style="filter:Gray;width: 26;height: 26;padding: 0px;background: url(<%=scanImagePath %>/rotateLeft.gif);" onclick="rotateLeft();">
                <input type="button" disabled title="顺时针旋转" id="btRotateRight" style="filter:Gray;width: 26;height: 26;padding: 0px;background: url(<%=scanImagePath %>/rotateRight.gif);" onclick="rotateRight();">
                <input type="button" disabled id="btDel"  title="删除" style="filter:Gray;width: 26;height: 26;padding: 0px;background: url(<%=scanImagePath %>/del.gif);" onclick="delImage();">
              </td>
              <td style="text-align: right;border: none;">
                <span id="currentImageIndex"></span>/<span id="imageCount"></span>
              </td>
            </tr>
          </table>
           
          </td>
       </tr>
       
       <tr>
         <th style="width:25%;font-size: 13px;">扫描仪：</th>
         <td style="width:50%">
         	<select id="source" >
         	  <option value="">请选择...</option>
         	</select>
         </td>
         <td style="width:25%;text-align: center">
           <input type="button" class="button" value="开始扫描" onclick="scan();">
               
           &nbsp;&nbsp;	 <input disabled id="btUpload" type="button" class="button" value="上传文件" onclick="upload();">
         </td>
         
       </tr>
       
 

     </table>
   </td>
 </tr>
</table>



<script language="javascript">

function refreshImageIndex(){
  currentImageIndex.innerHTML=webTwain.CurrentImageIndexInBuffer+1;
  imageCount.innerHTML=webTwain.HowManyImagesInBuffer;
}

function delImage(){
  if(!confirm("是否确认删除当前图片？"))return;
  webTwain.RemoveImage(webTwain.CurrentImageIndexInBuffer);
  setButtonsStatus();
  refreshImageIndex();
}

function rotateLeft(){
  webTwain.RotateLeft(webTwain.CurrentImageIndexInBuffer);
}

function rotateRight(){
  webTwain.RotateRight(webTwain.CurrentImageIndexInBuffer);
}
function setButtonDisable(button,value){
	button.disabled=value;
	button.style.filter=value?"Gray":"";
}

var initedSvrConf=null;
function getDomainConfig()
{
	if(initedSvrConf!=null){
		return initedSvrConf;
	}
	var currentUrl=	window.location.href;
	var conf={};
	var domainurl="";
	var sport="80";
	if(currentUrl.indexOf("www")==0){
		domainurl=currentUrl.split("/")[0];
	}else if(currentUrl.indexOf("http")==0){
		var urlsec= currentUrl.split("//")[1];
		domainurl=urlsec.split("/")[0];
	}
	if(domainurl.indexOf(":")!=-1){
		var dom = domainurl.split(":");
		conf = {s_domain:dom[0],s_port:dom[1]}; 	
	}else{
		conf = {s_domain:domainurl,s_port:sport};
	}
	initedSvrConf=conf;
	return conf;
}
			
			
function upload(){
  webTwain.HTTPPort=getDomainConfig().s_port;
  var isSuccess=false;
 var s_domain = getDomainConfig().s_domain;
 
 if(webTwain.HowManyImagesInBuffer==1)
	    isSuccess=webTwain.HTTPUploadThroughPost(s_domain,0,"<%= ctxPath%>/GetScanFile;jsessionid=<%=request.getSession().getId() %>","temp_compare.jpg");
  if(isSuccess){
	  this.opener.window.scanquery();
	  	window.close();
	 	//window.location.href = "<%=request.getContextPath()+"/pages/scan/scanResult.jsp"%>"
  }else{
   		alert("上传失败，请联系管理员");
  }
}

 
var retryTimes=0;
function init(){
  try{
	  webTwain.Zoom=500;
    webTwain.SetViewMode(-1,-1);
    for(var i=0;i<webTwain.SourceCount;i++){
      var sourceName=webTwain.SourceNameItems(i);
	  source.options.add(new Option(sourceName,String(i)));
    }
    if(webTwain.SourceCount>0){
	  source.selectedIndex=1;
    }
    refreshImageIndex();
  }catch(e){
    retryTimes++;
 	if(retryTimes>3)
 	  throw new Error("初始化失败");
 	else
 	  window.setTimeout(init,1000);
  }
}

function scan(){
  if(source.selectedIndex==0){
    alert("请选择扫描仪");
    return;
  }
  webTwain.CloseSource();
  webTwain.OpenSource();
  webTwain.IfShowUI =false;
  webTwain.IfDisableSourceAfterAcquire = true;
  webTwain.SelectSourceByIndex(Number(source.value));  
  webTwain.AcquireImage();
}

function setButtonsStatus(){
  if(webTwain.HowManyImagesInBuffer>0){
     
	setButtonDisable(btRotateLeft,false);
	setButtonDisable(btRotateRight,false);
	setButtonDisable(btDel,false);
	setButtonDisable(btUpload,false);
	
  }else{
	//setButtonDisable(btZoomOut,true);
   // setButtonDisable(btZoomDefault,true);		
	setButtonDisable(btRotateLeft,true);
	setButtonDisable(btRotateRight,true);
	setButtonDisable(btDel,true);
	setButtonDisable(btUpload,true);
  }
  if(webTwain.CurrentImageIndexInBuffer<webTwain.HowManyImagesInBuffer-1)
    setButtonDisable(btNext,false);
  else
    setButtonDisable(btNext,true);
    
  if(webTwain.CurrentImageIndexInBuffer>0)
    setButtonDisable(btPrevious,false);
  else
    setButtonDisable(btPrevious,true);
}

function previous(){
  webTwain.CurrentImageIndexInBuffer-=1;
  setButtonsStatus();
  refreshImageIndex();
}

function next(){
  webTwain.CurrentImageIndexInBuffer+=1;
  setButtonsStatus();
  refreshImageIndex();
}
function loadLoaclImage(){
	webTwain.IfShowFileDialog = true;
	webTwain.LoadImageEx("", 5);
}
//webTwain.CloseSource();
</script>
<script language="javascript" for="webTwain" event="OnPostTransfer">
	setButtonsStatus();
	refreshImageIndex();
	 
</script>
</body>
</html>
